//
//  SearchViewController.m
//  Edumap
//
//  Created by 辰领科技 on 16/8/8.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchTableViewCell.h"
#import "SchoolProfileViewController.h"
#import "DrivateTutorDetailViewController.h"
#import "AgencyDetailsViewController.h"
static NSString * CellIdentifier = @"SearchTableViewCell";

@interface SearchViewController ()
<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UITextField *textField;
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSArray *array;


@end

@implementation SearchViewController



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initViewLayout];
    
    [self search];
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    
    [_textField setValue:[UIColor whiteColor]
                  forKeyPath:@"_placeholderLabel.textColor"];
    
    [self.tableView registerNib:[UINib nibWithNibName:CellIdentifier bundle:nil] forCellReuseIdentifier:CellIdentifier];
    
    _textField.delegate = self;
    _textField.layer.cornerRadius = 3*VerticalRatio();
    _textField.leftView = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 8, 1), NO)];
    _textField.leftViewMode = UITextFieldViewModeAlways;
    
}

- (IBAction)back:(id)sender {
    [self backBtn];
}

#pragma mark - 删除文字
- (IBAction)delete:(id)sender {
    [_textField setText:@""];
}
#pragma mark - 搜索
- (IBAction)search:(id)sender {
    [_textField resignFirstResponder];
    [self search];
}

#pragma mark ==== UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self search];
    [_textField resignFirstResponder];
    return YES;
}
#pragma mark - 首页搜索
- (void)search {
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    if (_serachStr) {
        [dic setObject:_serachStr forKey:@"name"];
    }
    if (_textField.text.length>0) {
        [dic setObject:_textField.text forKey:@"name"];
    }
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:JY_indexList withHttpType:@"POST" withTag:JY_JY_indexList];
}

#pragma mark - <UITableViewDelegate&&UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//定义 UITableView cell 的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count;
}

//定义 UITableView cell 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45*VerticalRatio();
}

//定义 UITableView cell 展示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SearchTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell= [[SearchTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
    }
    for (UIView *view in cell.subviews) {
        if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    
    NSString *type = [NSString stringWithFormat:@"%@",_array[indexPath.row][@"type"]];
    NSString *Str;
    if ([type isEqualToString:@"1"]) {
        //家教
        Str = @"[家教]";
    } else if ([type isEqualToString:@"2"]) {
        //学校
        Str = @"[学校]";
    } else if ([type isEqualToString:@"3"]) {
        //机构
        Str = @"[机构]";
    }
    cell.label.text = [Str stringByAppendingString:_array[indexPath.row][@"name"]];
      
    return cell;
}

//定义 UITableView cell 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *type = [NSString stringWithFormat:@"%@",_array[indexPath.row][@"type"]];
    if ([type isEqualToString:@"1"]) {
        //家教
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeTeachStoryboard" bundle:nil];
        DrivateTutorDetailViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"DrivateTutorDetail"];
        page.mexpId = [NSString stringWithFormat:@"%@",_array[indexPath.row][@"id"]];
        page.backBlock = ^(){};
        [[Common getNavigation] pushViewController:page];
    } else if ([type isEqualToString:@"2"]) {
        //学校
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"SchoolStoryboard" bundle:nil];
        SchoolProfileViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"SchoolProfile"];
        page.schoolId = [NSString stringWithFormat:@"%@",_array[indexPath.row][@"id"]];
        page.backBlock = ^(){};
        [[Common getNavigation] pushViewController:page];
    } else if ([type isEqualToString:@"3"]) {
        //机构
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"TeachStoryboard" bundle:nil];
        AgencyDetailsViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"AgencyDetails"];
        page.mstoId = [NSString stringWithFormat:@"%@",_array[indexPath.row][@"id"]];
        page.backBlock = ^(){};
        [[Common getNavigation] pushViewController:page];
    }
}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{

    if (tag==JY_JY_indexList) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else {
            _array = [NSArray array];
            _array = database[@"result"][@"rows"];
            self.tableView.delegate = self;
            self.tableView.dataSource = self;
            [self.tableView reloadData];
        }
    }
    
}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{

    [self dismissWaitDialog];
    
}

@end
